package com.china08.yxyapi.api.manage.edu.platform.continuedu.renwu;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.continuedu.YxyContinueduLesson;
import com.china08.yxyapi.entity.db.continuedu.YxyContinueduTask;
import com.china08.yxyapi.entity.db.continuedu.YxyContinueduTaskLesson;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.continuedu.YxyContinueduLessonItemRepository;
import com.china08.yxyapi.repository.db.continuedu.YxyContinueduLessonRepository;
import com.china08.yxyapi.repository.db.continuedu.YxyContinueduTaskLessonRepository;
import com.china08.yxyapi.repository.db.continuedu.YxyContinueduTaskRepository;
import com.china08.yxyapi.util.DateToolUtils;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
public class YxyContinueduRenWuDetailController extends YxyApiBaseController {

    @Autowired
    YxyContinueduTaskRepository taskRepository;

    @Autowired
    YxyContinueduLessonRepository lessonRepository;

    @Autowired
    YxyContinueduTaskLessonRepository taskLessonRepository;

    @Autowired
    YxyContinueduLessonItemRepository itemRepository;

    @ApiOperation(value = "")
    @RequestMapping(value = "/api_yw/manage/edu/platform/continuedu/renwu/detail", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel precess(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {
        RespModel respModel = new RespModel();
        List<Pojo> lessons = new ArrayList<Pojo>();

        String taskId = reqModel.taskId;
        YxyContinueduTask task = taskRepository.findById(taskId);

        if (task != null) {
            respModel.taskName = task.getName();
            respModel.taskScore = task.getScore();
            respModel.completeCount = task.getCompleteCount();
            respModel.completeRate = task.getCompleteRate();
            Date startDate = task.getBeginDate();
            Date endDate = task.getEndDate();
            if (startDate != null) {
                respModel.startDate = startDate;
                respModel.startDateString = DateToolUtils.format(startDate, "yyyy-MM-dd");
            }
            if (endDate != null) {
                respModel.endDate = endDate;
                respModel.endDateString = DateToolUtils.format(endDate, "yyyy-MM-dd");
            }

        }

        List<YxyContinueduTaskLesson> lessonList = taskLessonRepository.findByTaskId(taskId);
        for (YxyContinueduTaskLesson lesson : lessonList) {
            Pojo pojo = new Pojo();
            pojo.resLessonId = lesson.getId();
            pojo.lessonId = lesson.getLessonId();
            pojo.lessonName = lesson.getLessonName();

            // 课程id
            String lId = lesson.getLessonId();
            pojo.itemCount = itemRepository.countByLessonId(lId);

            YxyContinueduLesson l = lessonRepository.findById(lId);
            Integer duration = l.getDuration();
            pojo.duration = disDuration(duration);
            lessons.add(pojo);
        }
        respModel.lessonList = lessons;

        return respModel;
    }

    public static class ReqModel extends YxyApiReqModel {
        public String taskId;
    }

    public static class Pojo {
        public String resLessonId;
        public String lessonId;
        public String lessonName;
        public Integer itemCount;
        public String duration;
    }

    public static class RespModel extends YxyApiRespModel {
        public String taskName;
        public Integer taskScore;
        public Date startDate;
        public Date endDate;
        public String startDateString;
        public String endDateString;
        public Long completeCount;
        public Double completeRate;
        public List<Pojo> lessonList;
    }

    public String disDuration(Integer miao) {
        if (miao == null || miao < 0) {
            miao = 0;
        }
        int h = miao / 3600;
        int m = (miao % 3600) / 60;
        int s = (miao % 3600) % 60;
        if (h > 0) {
            return (h + "时" + m + "分" + s + "秒");
        } else {
            if (m > 0) {
                return (m + "分" + s + "秒");
            } else {
                return (s + "秒");
            }
        }
    }
}
